home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_10.lha / 6_10 / 6_10out.c < prev    next >
Text File  |  1993-08-08  |  1KB  |  66 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. *
  6.    Output a type LINT to the given stream.
  7. /
  8. include <lint.h>
  9.  
  10. stream& operator<< (ostream& out, LINT j)
  11.  
  12.    out << form("0x%4.4x%4.4x%4.4x%4.4x, ",            /* DELETE */
  13.     j.s[0], j.s[1], j.s[2], j.s[3]);    /* DELETE */
  14.    LINT val = j;
  15.  
  16.    // reverse sign of negative numbers
  17.    if (j.isneg())
  18. {
  19. val = -j;
  20. out << "-";
  21. }
  22.  
  23.    // check for the easy single precision case
  24.    if (val.s[0] == 0 && val.s[1] == 0)
  25. {
  26. LINT_Ltype l = val.s[2] * LINT_base + val.s[3];
  27. out << l;
  28. }
  29.  
  30.    else
  31. {
  32. // output can be stored in 5 "hyper-decimal" digits,
  33. // each digit holding a value 0..9999.
  34. LINT_type a[5], *p = a;
  35. const LINT_type dec_base = 10000;
  36.  
  37. // store "digits" in reverse order
  38. do  {
  39.     // *p++ = val % 10000
  40.     // val /= 10000;
  41.     LINT_Ltype prevu = 0;
  42.  
  43.     for (int r = 0; r < 4; r++)
  44.     {
  45.     LINT_Ltype tmp = val.s[r] +
  46.         prevu * LINT_base;
  47.     LINT_type tmpq = tmp / dec_base;
  48.     val.s[r] = tmpq;    // % LINT_base
  49.     prevu = tmp - dec_base * tmpq;
  50.     }
  51.  
  52.     *p++ = prevu;
  53. } while (val.s[3] || val.s[2] ||
  54.      val.s[1] || val.s[0]);    // val != 0
  55.  
  56. // Output digits in forward order.
  57. // All but first digit must be 4 decimal
  58. // digits in lengths, including leading zeros.
  59. out << *--p;
  60. while (--p >= a)
  61.     out << form("%4.4u", *p);
  62. }
  63.  
  64.    return out;
  65.  
  66.